﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Collections.Specialized;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
using SadlandVillage.Core.Helper;

namespace SadlandVillage.Core
{

    public class Menu
    {
        // Sprite Batch
        protected SpriteBatch spriteBatch;
        // ContentManager
        protected ContentManager content;
        // Sprite Fonts
        protected SpriteFont regularFont, selectedFont;
        // Colors
        protected Color regularColor = Color.DarkGreen, selectedColor = Color.GreenYellow;
        public Color RegularColor { get { return regularColor; } set { regularColor = value; } }
        public Color SelectedColor { get { return selectedColor; } set { selectedColor = value; } }
        // Menu Position
        public Vector2 Position { get; set; }
        // Items
        public int SelectedIndex { get; set; }
        private readonly StringCollection menuItems;
        // Size of menu in pixels
        public int Width { get; private set; }
        public int Height { get; private set; }
        //Input
        private KeyboardState oldKeyboardState;
        private KeyboardState keyboardState;
        private Texture2D background;

        //Choice
        public event ItemChosenEventHandler<string> ItemChosen;

        //Soung
        private Song bgm;
        private SoundEffect click;
        

        public void SetMenuItems(string[] items)
        {
            this.menuItems.Clear();
            this.menuItems.AddRange(items);
            CalculateBounds();
        }

        private void CalculateBounds()
        {
            Width = 0;
            Height = 0;
            foreach (string item in menuItems)
            {
                Vector2 size = selectedFont.MeasureString(item);
                if (size.X > Width)
                {
                    Width = (int)size.X;
                }
                Height += selectedFont.LineSpacing;
            }
        }

        public Menu(ContentManager content, SpriteBatch spriteBatch)
        {
            menuItems = new StringCollection();

            this.spriteBatch = spriteBatch;
            this.content = content;

            oldKeyboardState = Keyboard.GetState();
        }

        public void LoadContent()
        {
            this.regularFont = content.Load<SpriteFont>(@"Menu\Selected");
            this.selectedFont = content.Load<SpriteFont>(@"Menu\Unselected");
            background = content.Load<Texture2D>(@"Menu\background");
            bgm = content.Load<Song>(@"Menu\game_bgm");
            click = content.Load<SoundEffect>(@"Menu\click");           
        }        

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            if (MediaPlayer.State == MediaState.Stopped)
            {
                MediaPlayer.IsRepeating = true;
                MediaPlayer.Play(bgm);
            }           

            bool down = Input.KeyPressed(Keys.Down);

            bool up = Input.KeyPressed(Keys.Up);

            bool selected = Input.KeyPressed(Keys.Enter);

            if (down)
            {
                SelectedIndex++;

                if (SelectedIndex == menuItems.Count)
                {
                    SelectedIndex = 0;
                }

                click.Play();

            }

            if (up)
            {
                SelectedIndex--;

                if (SelectedIndex == -1)
                {
                    SelectedIndex = menuItems.Count - 1;
                }
                click.Play();

            }

            if (selected)
            {
                if (this.ItemChosen != null)
                {
                    ItemChosen(menuItems[SelectedIndex]);
                }
            }          
        }

        public void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(background, Vector2.Zero, Color.White);
            spriteBatch.End();

            float y = Position.Y;
            for (int i = 0; i < menuItems.Count; i++)
            {
                SpriteFont font;
                Color theColor;
                if (i == SelectedIndex)
                {
                    font = selectedFont;
                    theColor = selectedColor;
                }
                else
                {
                    font = regularFont;
                    theColor = regularColor;
                }

                // Draw the text shadow
                spriteBatch.Begin();
                StringEffects.PrintShadowedString(spriteBatch, font, menuItems[i], new Vector2(Position.X, y), theColor, Color.Black, 1);

                y += font.LineSpacing;

                spriteBatch.End();
            }
        }
    }
    
}
